package com.zhoug.demo.zxing;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.zxing.utils.ZxingHelper;

import java.util.Map;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;


public class MainActivity extends MyBaseActivity {
    private ActivityResultLauncher<String[]> mPerLauncher;
    private ZxingHelper mZxingHelper;
    private Button mBtnScan;
    private TextView mTvContent;
    private SwitchCompat mSwitchSearch;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mBtnScan = findViewById(R.id.btn_scan);
        mTvContent = findViewById(R.id.tv_content);
        mSwitchSearch = findViewById(R.id.switch_search);
    }


    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mZxingHelper = ZxingHelper.registerForActivityResult(this);
        mZxingHelper.setSearchEnable(true)
                .setChooseImageEnable(true)
                .setTitle("扫码追溯")
                .setCallback(new ZxingHelper.Callback() {
                    @Override
                    public void result(int code, String msg) {
                        mTvContent.setText(String.format("%s:%s", code == ZxingHelper.Callback.CODE_SUCCESS ? "成功" : "失败", msg));
                    }
                });

        mPerLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                boolean a = true;
                if (result != null && !result.isEmpty()) {
                    for (String item : result.keySet()) {
                        Boolean b = result.get(item);
                        if (b == null || !b) {
                            a = false;
                        }
                    }
                }
                if (a) {
                    mZxingHelper.setSearchActivityClassName(mSwitchSearch.isChecked() ? TestSearchActivity.class.getCanonicalName() : null);
                    mZxingHelper.start(getActivity());
                } else {
                    Toast.makeText(getActivity(), "权限申请失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        mBtnScan.setOnClickListener(v -> {
            mPerLauncher.launch(new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE});
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       /* if (mZxingHelper != null) {
            mZxingHelper.onActivityResult(requestCode, resultCode, data);
        }*/
    }

}